<link rel="stylesheet" href="${request.contextPath}/lib/asmpWorkCss/ZtreeInput.css">
<link rel="stylesheet" href="${request.contextPath}/css/diy/font.css">
<div class="box box-primary">
    <div class="box-body" style="width: 100%">
        <div class="row">
            <div class="col-xs-3">
                <div class="input-group">
                    <span class="input-group-addon" id="sizing-addon2">条件1</span>
                    <input type="text" class="form-control" id="carbrand"
                           aria-describedby="sizing-addon1">
                </div>
            </div>
            <div class="col-xs-3">
                <div class="input-group">
                    <span class="input-group-addon" id="sizing-addon2">条件1</span>
                    <input type="text" class="form-control" id="carbrand2"
                           aria-describedby="sizing-addon1">
                </div>
            </div>
            <div class="col-xs-1">
                <button class="btn btn-default" onclick="condictionsSearch()">
                    <i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
    <div class="box-header with-border">
        <div id="mainpanel" style="margin: 0px 0px 0px 0px;">
            <div id="mapcontent" style="width: 100%;position: relative;">
                <div id="btn_list">

                </div>
                <div class="box-header with-border">
                    <div class="box-body" style="width: 100%">
                        <div class="row">
                            <#list btnlist as items>
                                <button class="${items.funcBg!}" id="${items.funcCode!}">
                                    <i class="${items.funcIcon!}"></i> ${items.funcName!}
                                </button>

                            </#list>

                        </div>
                    </div>
                </div>
                <div class="box-body">
                    <table width="100%" class="table table-bordered table-striped" id="data_table">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade bs-example-modal-mid" id="myModal" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-mid">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">正在加载...</h4>
            </div>
            <div class="modal-body">正在加载...</div>
            <div class="modal-footer" id="modal-footer1">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button id="myModalSave" type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="${request.contextPath}/js/addArea.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        search();
    });
    var table;

    function search() {
        var params = {};
        table = $('#data_table').DataTable({
            "ajax": {
                "url": contextPath + "/soa/baseinfo/customer/datas/search",
                "type": "post",
                "dataType": "json",
                "data": function (data) {
                    var param = {
                        "params": {},
                        "start": data.start,
                        "length": data.length
                    };
                    return param;
                }
            },
            pageLength: 8,
            "language": {
                "sProcessing": "正在加载中......",
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sZeroRecords": "对不起，查询不到相关数据！",
                "sEmptyTable": "表中无数据存在！",
                "search": "查询:",
                "paginate": {
                    "previous": "上一页",
                    "next": "下一页",
                    "first": "第一页",
                    "last": "最后一页"
                }
            },
            paging: true,
            lengthChange: false,
            searching: false,
            ordering: false,
            search: false, //显示搜索框
            bLengthChange: true,
            info: true,
            autoWidth: true,
            serverSide: true,
            processing: true,
            columns: [
                {
                    title: '<input name="select_all" type="checkbox">',
                    width: "1%",
                    data: function (e) {//这里给最后一列返回一个操作列表
                        return '<input value="' + e.id + '" type="checkbox">';
                    }
                }
                , {
                    title: "公司名称",
                    data: 'companyName'
                }
                , {
                    title: "统一社会信用代码",
                    data: 'companyCode'
                }
                , {
                    title: "公司类型",
                    data: 'companyType'
                }
                , {
                    title: "注册资本",
                    data: 'companyCapital'
                }
                , {
                    title: "成立时间",
                    data: 'companyCreateTime'
                }
                , {
                    title: "联系人",
                    data: 'companyContact'
                }
                , {
                    title: "联系电话",
                    data: 'companyContactPhone'
                }
            ]

        });
    }
    //条件搜索
    function condictionsSearch() {
        //条件搜索
        var params = {};
        var url = contextPath + "/soa/baseinfo/customer/datas/search";
        var floorid = $("#floorid").val();
        params.floorid = floorid;
        params.areaId = $("#region").attr('data_id');
        //aboutForm.js里
        refreshData(url, params);
    }

    //初始表格


    //-------------------------------------------以下为复选框相关js-----------------------------------------


    //处理 checkbox 点击事件
    $('#data_table tbody').on('click', 'input[type="checkbox"]', function (e) {
        var $row = $(this).closest('tr');

        // 获取行数据
        var data = table.row($row).data();

        // 获取行id
        var rowId = data[0];

        //
        var index = $.inArray(rowId, rows_selected);

        // If checkbox is checked and row ID is not in list of selected row IDs
        if (this.checked && index === -1) {
            rows_selected.push(rowId);

            // Otherwise, if checkbox is not checked and row ID is in list of selected row IDs
        } else if (!this.checked && index !== -1) {
            rows_selected.splice(index, 1);
        }

        if (this.checked) {
            $row.addClass('selected');
        } else {
            $row.removeClass('selected');
        }

        //更新表格选择状态
        updateDataTableSelectAllCtrl(table);

        e.stopPropagation();
    });
    // 表格行选择数组
    var rows_selected = [];
    //更新表格 单选 全选状态
    function updateDataTableSelectAllCtrl(table) {
        var $table = table.table().node();
        var $chkbox_all = $('tbody input[type="checkbox"]', $table);
        var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);
        var chkbox_select_all = $('thead input[name="select_all"]', $table).get(0);

        // 没有选中
        if ($chkbox_checked.length === 0) {
            chkbox_select_all.checked = false;
            if ('indeterminate' in chkbox_select_all) {
                chkbox_select_all.indeterminate = false;
            }

            // 全部选中
        } else if ($chkbox_checked.length === $chkbox_all.length) {
            chkbox_select_all.checked = true;
            if ('indeterminate' in chkbox_select_all) {
                chkbox_select_all.indeterminate = false;
            }

            // 部分选中
        } else {
            chkbox_select_all.checked = true;
            if ('indeterminate' in chkbox_select_all) {
                chkbox_select_all.indeterminate = true;
            }
        }
    }
    // 处理表格单元格单击复选框 如果需要...
    $('#data_table').on('click', 'tbody td, thead th:first-child', function (e) {
        $(this).parent().find('input[type="checkbox"]').trigger('click');
    });

    // 表格全选
    $('thead input[name="select_all"]', table.table().container()).on(
            'click',
            function (e) {
                if (this.checked) {
                    $('#data_table tbody input[type="checkbox"]:not(:checked)')
                            .trigger('click');
                } else {
                    $('#data_table tbody input[type="checkbox"]:checked').trigger(
                            'click');
                }

                e.stopPropagation();
            });

    // on是开启监听--作用要问兴斌
    table.on('draw', function () {
        updateDataTableSelectAllCtrl(table);
    });


    //-------------------------------------------以上为复选框相关js-----------------------------------------

    //刷新 重新载入分页数据
    //刷新用户表到初始
    function DTrefresh() {
        var url = contextPath + "/soa/baseinfo/customer/datas/search";
        var params = {};
        table.settings()[0].ajax.url = url;
        table.settings()[0].ajax.data = {"params": params};
        setTimeout(function () {
            table.ajax.reload();
        }, 200);
    }


    //隐藏之前清除数据
    $("#myModal").on("hidden.bs.modal", function () {
        // console.log("清除数据");
        // $("#myModal").removeData("bs.modal");
        $("#myModal").find(".modal-body").empty();
        $("#myModalSave").off('click');//移除点击事件
    });
    //摸态框显示出来之后
    $("#myModal").on("shown.bs.modal", function () {
        $("#code").focus();
    });


    //确定保存
    function save() {
        $('#form_submit').data('bootstrapValidator').validate();
        var flag = $('#form_submit').data('bootstrapValidator').isValid();
        if (!flag) {
            return;
        }
        var count = 0;
        var companyRate="";
        $("#companyRate-table tr").each(function(){
            var name = $(this).find(".companyRateName").eq(0).val();
            var rate=$(this).find(".companyRateValue").eq(0).val();
            if(name!=""){
                if(count == 0){
                    companyRate=companyRate+name+":"+rate;
                }
                else{
                    companyRate=companyRate+","+name+":"+rate;
                }
                count++;
            }
        });
        if(companyRate!="");
        $("#companyRate").val(companyRate);
        ajaxSubmit($('#form_submit')[0], function (data) {
            if (data.success == 0) {
                info(data.msg);
                modalHide();
            } else {
                warning(data.msg);
            }
        });
    }
    function modalHide() {
        $('#myModal').modal('hide');
        DTrefresh();
    }
    //新增
    $('#add').on('click', function () {
        var modal = $('#myModal');
        modal.find('.modal-title').text("新增");
        modal.find('.modal-body').load(contextPath + "/soa/baseinfo/customer/inst", "", function (obj) {
            // console.log("进入后台");
        });
        $('#myModal').modal({
            backdrop: false
        });
        //绑定确定保存事件
        $("#myModalSave").on('click', function () {
            save();
        });
    });

    //编辑
    $('#edit').on('click', function () {
        var $table = table.table().node();
        var $chkbox_all = $('tbody input[type="checkbox"]', $table);
        var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);
        if ($chkbox_checked.length < 1) {
            var d = dialog({
                title: '提示',
                content: '请勾选要修改的记录！'
            });
            d.show();
            setTimeout(function () {
                d.close().remove();
            }, 2000);
        } else if ($chkbox_checked.length > 1) {
            var d = dialog({
                title: '提示',
                content: '请不要勾选多条记录修改！'
            });
            d.show();
            setTimeout(function () {
                d.close().remove();
            }, 2000);
        } else {
            //到这里会保证只有一条选中的行
            var selectRows = table.rows('.selected').data();
            //如果不是待提交状态或者审核不通过状态 则无法进行修改
            // if(selectRows[0].status!=0 && selectRows[0].status!=3){
            // 	layer.msg('暂无修改权限！', function(){
            // 	});
            // 	return ;
            // }

            var data = {"id": $chkbox_checked.val()};
            var id = $chkbox_checked.val();

            var modal = $('#myModal');
            modal.find('.modal-title').text("修改");
            modal.find('.modal-body').load(contextPath + "/soa/baseinfo/customer/edit", data, function (obj) {
            });
            $('#myModal').modal({
                backdrop: false
            });
            //绑定确定保存事件
            $("#myModalSave").on('click', function () {
                save();
            });
        }


    });


    //删除
    $('#del').on('click', function () {
        var $table = table.table().node();
        var $chkbox_all = $('tbody input[type="checkbox"]', $table);
        var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);

        var data = {"id": $chkbox_checked.val()};
        var id = $chkbox_checked.val();

        if ($chkbox_checked.length < 1) {
            var t = dialog({
                title: '提示',
                content: '请勾选要删除的记录！'
            });
            t.show();
            setTimeout(function () {
                t.close().remove();
            }, 2000);
        } else if ($chkbox_checked.length > 1) {
            var t = dialog({
                title: '提示',
                content: '暂无删除多条记录权限'
            });
            t.show();
            setTimeout(function () {
                t.close().remove();
            }, 2000);
        } else {
            var d = dialog({
                title: '提示',
                content: '确认删除id为：' + id + ' 的这条记录？',
                okValue: '确认',
                ok: function () {
                    var that = this;
                    var url = "/soa/baseinfo/customer/datas/del";
                    delObject(id, url);
                    setTimeout(function () {
                        DTrefresh();
                        that.close().remove();
                    }, 200);

                },
                cancelValue: '取消',
                cancel: function () {

                }

            });
            d.showModal();
        }


    });
    $('#data_table tbody').on('click', 'tr', function () {
        $(this).toggleClass('selected');
    });
</script>